<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('项目名称')" prop="listedDealId">
            <el-select v-model="form.listedDealId" filterable clearable :placeholder="$t('请选择')">
              <el-option
                v-for="item in listedDealOptions"
                :key="item.id"
                :label="item.label"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('公告类型')" prop="noticeType">
            <el-select v-model="form.noticeType" value="" :placeholder="$t('公告类型')" clearable>
              <el-option
                v-for="dict in noticeTypeOptions"
                :key="dict.dictValue"
                :label="$t(dict.dictLabel)"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('公告日期')" prop="noticeDate">
            <el-date-picker
              v-model="form.noticeDate"
              clearable
              style="width: 200px"
              type="date"
              value-format="yyyy-MM-dd"
              :placeholder="$t('公告日期')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('备注')" prop="remark">
            <el-input v-model="form.remark" type="textarea" :placeholder="$t('备注')" show-word-limit maxlength="2000" />
          </el-form-item>
        </el-col>
      </el-row>
      <FileTable :list.sync="form.fileList" data-type="listed_regular_notice" :btn-show="!otherBtnHide" :business-id="dealId" />
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存 取消按钮-->
      <save-btn has-permi="listed:regularNotice" />
    </div>
  </div>
</template>

<script>
    import { saveListedRegularNotice, getListedRegularNotice } from '@/api/deal/listed/regularNotice/listedRegularNotice'
    import { getListedDeal, getListedDealList } from '@/api/deal/listed/base/listedDeal'
    export default {
        data() {
            return {
                // 表单参数
                form: {
                  fileList: []
                },
                // 遮罩层
                loading: false,
                // 是否隐藏其它按钮
                otherBtnHide: true,
                dealId: undefined,
                // 公告类型
                noticeTypeOptions: [],
                // 上市项目
                listedDealOptions: [],
                // 表单校验
                rules: {
                  listedDealId: [{ required: true, message: this.$t('必填信息'), trigger: 'change' }],
                  noticeType: [{ required: true, message: this.$t('必填信息'), trigger: 'change' }],
                  noticeDate: [{ required: true, message: this.$t('必填信息'), trigger: 'change' }]
                }
            }
        },
        watch: {
          'form.listedDealId': {
            handler(val) {
              if (val !== null && val !== undefined && val !== '') {
                this.otherBtnHide = false
                this.selectTree(val)
              } else {
                this.otherBtnHide = true
              }
            },
            deep: true,
            immediate: true
          }
        },
        created() {
          this.getDicts('notice_type').then(response => {
            this.noticeTypeOptions = response.data
          })
          // 获取所有上市项目
          getListedDealList({ dealType: (this.$route.query.dealType === undefined ? 'equity' : this.$route.query.dealType) }).then(response => {
            this.listedDealOptions = response.rows
          })
          this.$bus.$emit('async_done')
        },
        methods: {
            // 初始化数据
            init(data) {
              this.loading = true
              if (data != null && data.id !== undefined) {
                    getListedRegularNotice(data.id).then(response => {
                        this.form = response.data
                        this.loading = false
                    })
                } else {
                    this.reset()
                    this.loading = false
              }
            },
            // 表单重置
            reset() {
                this.form = {
                    id: undefined,
                    dealType: (this.$route.query.dealType === undefined ? 'equity' : this.$route.query.dealType),
                    listedDealId: undefined,
                    noticeType: undefined,
                    noticeDate: undefined,
                    remark: undefined,
                    createBy: undefined,
                    createTime: undefined,
                    updateBy: undefined,
                    updateTime: undefined,
                    delFlag: undefined,
                    fileList: []
                }
                this.resetForm('form')
            },
          // 选择上市项目事件，获取选择的项目的其他关联数据
          selectTree(val) {
            // 获取关联数据
            getListedDeal(val).then(res => {
              this.dealId = res.data.dealId
            })
          },
            // 取消按钮
            cancel() {
                this.$parent.$parent.formOpen = false
                this.$parent.$parent.reset()
            },
            // 关闭form页面
            closeForm() {
                // 关闭form页面遮罩层
                this.loading = false
                // 关闭form页面
                if (this.$parent.$parent.formOpen !== undefined) {
                  this.$parent.$parent.formOpen = false
                }
                // 刷新list页面
                if (this.$parent.$parent.getList !== undefined) {
                  this.$parent.$parent.getList()
                }
            },
            /** 提交按钮 */
            submitForm: function() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        this.loading = true
                        saveListedRegularNotice(this.form).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess(this.$t('保存成功'))
                                this.closeForm()
                            } else {
                                this.msgError(response.msg)
                            }
                            this.loading = false
                        })
                    } else {
                       this.locationError(document)
                    }
                })
           }

        }

    }
</script>
